package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.IotAlarmTriggerConditionRpcService;
import com.hunttown.mes.rpc.domain.IotAlarmTriggerConditionDTO;
import com.hunttown.mes.rpc.domain.query.IotAlarmTriggerConditionDTOQuery;
import com.hunttown.mes.service.IotAlarmTriggerConditionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.IotAlarmTriggerConditionConverts.convert;

/**
 * created by wangjunfu on 2021-03-04 19:17:13
 */
@Service
public class IotAlarmTriggerConditionRpcServiceImpl implements IotAlarmTriggerConditionRpcService {

    private final Logger logger = LoggerFactory.getLogger(IotAlarmTriggerConditionRpcServiceImpl.class);

    private final IotAlarmTriggerConditionService iotAlarmTriggerConditionService;

    @Autowired
    public IotAlarmTriggerConditionRpcServiceImpl(IotAlarmTriggerConditionService iotAlarmTriggerConditionService) {
        this.iotAlarmTriggerConditionService = iotAlarmTriggerConditionService;
    }

    @Override
    public IotAlarmTriggerConditionDTO insert(IotAlarmTriggerConditionDTO iotAlarmTriggerConditionDTO) {
        try {
            return convert(iotAlarmTriggerConditionService.insert(convert(iotAlarmTriggerConditionDTO)));
        } catch (Exception e) {
            logger.error("IotAlarmTriggerCondition create failed.", e);
            throw new RuntimeException("IotAlarmTriggerCondition create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(IotAlarmTriggerConditionDTO iotAlarmTriggerConditionDTO) {
        try {
            return iotAlarmTriggerConditionService.updateInfoById(convert(iotAlarmTriggerConditionDTO));
        } catch (Exception e) {
            logger.error("IotAlarmTriggerCondition update by id failed.", e);
            throw new RuntimeException("IotAlarmTriggerCondition update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(IotAlarmTriggerConditionDTOQuery iotAlarmTriggerConditionDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (iotAlarmTriggerConditionDTOQuery.getW_id() != null && iotAlarmTriggerConditionDTOQuery.getW_id() > 0) {
                query.put("w_id", iotAlarmTriggerConditionDTOQuery.getW_id());
            }

            if (iotAlarmTriggerConditionDTOQuery.getIds() != null && !iotAlarmTriggerConditionDTOQuery.getIds().equals("")) {
                query.put("ids", iotAlarmTriggerConditionDTOQuery.getIds());
            }

            if (iotAlarmTriggerConditionDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", iotAlarmTriggerConditionDTOQuery.getStopFlag());
            }

            if (iotAlarmTriggerConditionDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", iotAlarmTriggerConditionDTOQuery.getDeleteFlag());
            }

            return iotAlarmTriggerConditionService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("IotAlarmTriggerCondition update by query failed.", e);
            throw new RuntimeException("IotAlarmTriggerCondition update by query failed, Please order your code.", e);
        }
    }

    @Override
    public IotAlarmTriggerConditionDTO selectById(Integer id) {
        try {
            return convert(iotAlarmTriggerConditionService.selectById(id));
        } catch (Exception e) {
            logger.error("IotAlarmTriggerCondition select by id failed.", e);
            throw new RuntimeException("IotAlarmTriggerCondition select by id failed, Please order your code.", e);
        }
    }

    @Override
    public IotAlarmTriggerConditionDTO selectObjByQuery(IotAlarmTriggerConditionDTOQuery iotAlarmTriggerConditionDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (iotAlarmTriggerConditionDTOQuery.getEntId() != null) {
                query.put("entId", iotAlarmTriggerConditionDTOQuery.getEntId());
            }

            if (iotAlarmTriggerConditionDTOQuery.getEquipId() != null) {
                query.put("equipId", iotAlarmTriggerConditionDTOQuery.getEquipId());
            }

            if (iotAlarmTriggerConditionDTOQuery.getIsOpen() != null) {
                query.put("isOpen", iotAlarmTriggerConditionDTOQuery.getIsOpen());
            }

            if (iotAlarmTriggerConditionDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", iotAlarmTriggerConditionDTOQuery.getStopFlag());
            }

            return convert(iotAlarmTriggerConditionService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("IotAlarmTriggerCondition select obj by query failed.", e);
            throw new RuntimeException("IotAlarmTriggerCondition select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<IotAlarmTriggerConditionDTO> getListForPage(IotAlarmTriggerConditionDTOQuery iotAlarmTriggerConditionDTOQuery) {
        Query query = Query.page(iotAlarmTriggerConditionDTOQuery.getPn(), iotAlarmTriggerConditionDTOQuery.getPageSize());

        if (iotAlarmTriggerConditionDTOQuery.getId() != null && iotAlarmTriggerConditionDTOQuery.getId() > 0) {
            query.put("id", iotAlarmTriggerConditionDTOQuery.getId());
        }

        if (iotAlarmTriggerConditionDTOQuery.getIds() != null && !iotAlarmTriggerConditionDTOQuery.getIds().equals("")) {
            query.put("ids", iotAlarmTriggerConditionDTOQuery.getIds());
        }

        if (iotAlarmTriggerConditionDTOQuery.getEntId() != null) {
            query.put("entId", iotAlarmTriggerConditionDTOQuery.getEntId());
        }

        if (iotAlarmTriggerConditionDTOQuery.getEquipId() != null) {
            query.put("equipId", iotAlarmTriggerConditionDTOQuery.getEquipId());
        }

        if (iotAlarmTriggerConditionDTOQuery.getEquipType() != null) {
            query.put("equipType", iotAlarmTriggerConditionDTOQuery.getEquipType());
        }

        if (iotAlarmTriggerConditionDTOQuery.getIsOpen() != null) {
            query.put("isOpen", iotAlarmTriggerConditionDTOQuery.getIsOpen());
        }

        if (StringUtils.isNotBlank(iotAlarmTriggerConditionDTOQuery.getLikeName())) {
            query.put("likeName", iotAlarmTriggerConditionDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(iotAlarmTriggerConditionDTOQuery.getShowDate())) {
            query.put("showDate", iotAlarmTriggerConditionDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(iotAlarmTriggerConditionDTOQuery.getStartDate())) {
            query.put("startDate", iotAlarmTriggerConditionDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(iotAlarmTriggerConditionDTOQuery.getEndDate())) {
            query.put("endDate", iotAlarmTriggerConditionDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(iotAlarmTriggerConditionDTOQuery.getOrderBy())) {
            query.put("orderBy", iotAlarmTriggerConditionDTOQuery.getOrderBy());
        }

        if (iotAlarmTriggerConditionDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", iotAlarmTriggerConditionDTOQuery.getStopFlag());
        }

        if (iotAlarmTriggerConditionDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", iotAlarmTriggerConditionDTOQuery.getDeleteFlag());
        }

        List<IotAlarmTriggerConditionDTO> list = convert(iotAlarmTriggerConditionService.selectByQuery(query));
        Page<IotAlarmTriggerConditionDTO> page = Page.from(iotAlarmTriggerConditionDTOQuery.getPn(), iotAlarmTriggerConditionDTOQuery.getPageSize(), list);
        page.setTotalItems(iotAlarmTriggerConditionService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return iotAlarmTriggerConditionService.deleteById(id);
        } catch (Exception e) {
            logger.error("IotAlarmTriggerCondition delete failed.", e);
            throw new RuntimeException("IotAlarmTriggerCondition delete failed, Please order your code.", e);
        }
    }
}